package edu.mis.modules.product;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import edu.mis.model.Product;

public class ProductController extends Controller {

	public void index() {
		render("product.html");
	}
	public void list() {
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		Page<Product> data = Product.dao.paginate(page, rows);
		setAttr("rows", data.getList());
		setAttr("total", data.getTotalRow());
		renderJson();
	}

	public void add() throws Exception {
		Model<Product> product = getModel(Product.class,"");
		if (product.save()) {
			setAttr("success",true);
			setAttr("msg","添加产品成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","添加产品失败!");
		}
		renderJson();
	}
	
	public void edit() throws Exception {
		Model<Product> product= this.getModel(Product.class,"");
		if (product.update()) {
			setAttr("success",true);
			setAttr("msg","修改产品成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","修改产品失败!");
		}
		renderJson();
	}

	public void delete() throws Exception {
		String ids=getPara("ids");
		if(Product.dao.deleteBatch(ids)){ 
			setAttr("success",true);
			setAttr("msg","删除产品成功!");
		}else{
			setAttr("success",true);
			setAttr("msg","删除产品失败!");
		}
		renderJson();
	}
}
